import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/keyboard_close.dart';
import 'package:sainbainu/common/widgets/widget.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import 'index.dart';

class OfficialAuthPage extends GetView<OfficialAuthController> {
  const OfficialAuthPage({Key? key}) : super(key: key);

  Widget _buildInput({
    required String labelText,
    TextEditingController? controller,
    bool readOnly = false,
  }) {
    return Container(
      decoration: const BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      clipBehavior: Clip.antiAlias,
      child: TextField(
        controller: controller,
        readOnly: readOnly,
        decoration: InputDecoration(
          border: InputBorder.none,
          fillColor: AppColors.page,
          filled: true,
          isDense: true,
          labelText: labelText,
          labelStyle: const TextStyle(color: AppColors.active),
          floatingLabelStyle: const TextStyle(color: AppColors.success),
        ),
        keyboardType: TextInputType.text,
        textInputAction: TextInputAction.go,
        cursorColor: AppColors.active,
        cursorErrorColor: AppColors.active,
        cursorHeight: 16,
      ),
    );
  }

  Widget _buildWrapper(List<Widget> children) {
    return SingleChildScrollView(
      physics: const BouncingScrollPhysics(),
      child: Padding(
        padding: const EdgeInsets.all(15),
        child: Container(
          padding: const EdgeInsets.all(15),
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: Column(
            children: children,
          ),
        ),
      ),
    );
  }

  Widget _buildUploadCard({
    required String labelImage,
    required String labelText,
    required String imageUrl,
    required VoidCallback onBoxTap,
    required VoidCallback onImageTap,
  }) {
    return GestureDetector(
      onTap: () => imageUrl.isEmpty ? onBoxTap() : onImageTap(),
      child: Container(
        height: 190,
        width: double.infinity,
        decoration: const BoxDecoration(
          color: AppColors.page,
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        clipBehavior: Clip.antiAlias,
        child: imageUrl.isEmpty
            ? Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(labelImage, width: 49, height: 49),
                  const SizedBox(height: 5),
                  Text(
                    labelText,
                    style: const TextStyle(color: AppColors.unactive),
                  ),
                ],
              )
            : CustomImage(url: imageUrl, fit: BoxFit.fill),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OfficialAuthController>(
      builder: (_) {
        return KeyboardClose(
          child: Scaffold(
            appBar: AppBar(title: Text("官方认证".tr)),
            body: _buildWrapper([
              _buildInput(
                labelText: '输入认证名称'.tr,
                controller: _.state.nameController,
                readOnly: _.state.isOfficial,
              ),
              const SizedBox(height: 15),
              Obx(
                () => _buildUploadCard(
                  labelImage: 'assets/images/auth_image.png',
                  labelText: '${'请上传证明资料'.tr}-1',
                  imageUrl: _.state.authNetImage1.value ?? '',
                  onBoxTap: () => _.handleSelectAuthImage1(),
                  onImageTap: () => _.handleShowAuthImage1Dialog(),
                ),
              ),
              const SizedBox(height: 15),
              Obx(
                () => _buildUploadCard(
                  labelImage: 'assets/images/auth_image.png',
                  labelText: '${'请上传证明资料'.tr}-2',
                  imageUrl: _.state.authNetImage2.value ?? '',
                  onBoxTap: () => _.handleSelectAuthImage2(),
                  onImageTap: () => _.handleShowAuthImage2Dialog(),
                ),
              ),
              const SizedBox(height: 30),
              SizedBox(
                width: double.infinity,
                height: 50,
                child: ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColors.danger,
                    foregroundColor: Colors.white,
                  ),
                  onPressed: _.state.isOfficial ? null : () => _.handleSubmit(),
                  child:
                      _.state.isOfficial ? const Text('已认证') : Text('确认提交'.tr),
                ),
              ),
            ]),
          ),
        );
      },
    );
  }
}
